# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=ccache
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.7.9
pkgrel=1
pkgdesc="Compiler cache that speeds up recompilation by caching previous compilations (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://ccache.dev/"
license=("GPL3")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' 'strip')
source=("https://github.com/ccache/ccache/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.xz"{,.asc}
        "01-directory-separator.patch")
sha256sums=('f893da7543bfb9172bd55e603fcbdfcd83e6def176a28689c13235695b4cf44b'
            'SKIP'
            '888748bd62a23b909dda4df714ffb2bb09db3fa698d199754f372a01b5a55179')
validpgpkeys=('5A939A71A46792CF57866A51996DDA075594ADB8') # Joel Rosdahl <joel@rosdahl.net>

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/01-directory-separator.patch"
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST}  && cd ${srcdir}/build-${MINGW_CHOST}
  ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST}
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  # hack: use bash scripts as shortcuts since we cannot use symlinks
  install -d ${pkgdir}${MINGW_PREFIX}/lib/ccache/bin
  cd ${pkgdir}${MINGW_PREFIX}/lib/ccache/bin
  scripts=(c++ cc cpp gcc g++ ${MINGW_CHOST}-g++ ${MINGW_CHOST}-c++ ${MINGW_CHOST}-gcc)
  for fn in ${scripts[*]}; do
    echo -e '#!/bin/bash\n\nccache '${MINGW_PREFIX}'/bin/'$fn' "$@"' > $fn
  done
}
